/*
 * Copyright (c) 2020-2099 the original author or authors. All rights reserve.
 */
package org.tianyun.cloud.datasource.routing.transaction;

import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.CannotCreateTransactionException;
import org.springframework.transaction.TransactionDefinition;
import org.tianyun.cloud.datasource.routing.DataSourceContext;

/**
 * RoutingDataSourceTransactionManager
 *
 * @auther ebert_chan
 */
public class RoutingDataSourceTransactionManager extends DataSourceTransactionManager {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 1L;

    @Override
    protected void doBegin(Object transaction, TransactionDefinition definition) {
        DataSourceContext context = DataSourceContext.get();
        // 从库数据源不能开启事务
        if (context != null && context.isSlave()) {
            throw new CannotCreateTransactionException("Could not open JDBC Connection for transaction in slave database");
        }
        super.doBegin(transaction, definition);
    }

}